#include "board_base.h"

#include "bsp_uart.h"
//#include "SEGGER_RTT.h"
#include "ITM.h"
#include "tim.h"
#include "gpio.h"
#include "adc.h"
#include "task_main.h"
#include "bsp_hrtimer.h"
#include "stimer.h"
#include "bsp_flash_config.h"
#include "math.h"
//#include "log.h"
//#include "w25qxx.h"
/*-----------------------------------------------------------------------
|                                define                                 |
-----------------------------------------------------------------------*/
//#define BOARDTAG "Board"
//#define Board_Firmware_ProjectName "STM32F030C8T6_Ammeter_Project"
//#define Board_Firmware_Version  STR(1.0)
//#define Board_Firmware_Auther  "liuzhihua"
//#define Board_System_Frequnce   SystemCoreClock
//const char LINESTR[] = "--------------------------------------------------------------------------------\n";
//#define STR(str) #str


#define BSP_GET_TICK()      HAL_GetTick()   // 低精度计数(增计数)
#define BSP_TICK_CNT_BIT    32
#define BSP_TICK_MAX_CNT    ((uint32_t)(((((1UL<<(BSP_TICK_CNT_BIT-1))-1)<<1)+1)))
#define BSP_GET_HPTICK()    (71999 - SysTick->VAL) // 高精度计数(增计数)
#define BSP_HPTICK_MAX_CNT  SysTick->LOAD
#define BSP_HPTICK2TICK(x)  (x/(SysTick->LOAD + 1))
#define BSP_TICK2HPTICK(x)  (x*(SysTick->LOAD + 1))
/*-----------------------------------------------------------------------
|                               Function                                |
-----------------------------------------------------------------------*/

uint16_t ADC1Buf[ADC1_BUF_SIZE];
uint16_t ADC2Buf[ADC2_BUF_SIZE];
/**
 * @brief 记录归零，开始测量时间
 * @param timecnt 用来记录时间的变量
 * @retval uint32_t 
 */
void bsp_MesureTimeStart(uint32_t *timecnt)
{
    *timecnt = BSP_GET_TICK();
}

/**
 * @brief 测量时间
 * @param old_counter 用来记录时间的变量
 * @retval uint32_t 上次使用到这次使用的时间(tick)
 */
uint32_t bsp_MesureTime(uint32_t *old_counter)
{
    uint32_t counter, couter_current;
    couter_current = BSP_GET_TICK();
    if (couter_current > *old_counter)//没有溢出
        counter = couter_current - *old_counter;
    else//发生过溢出
        counter = couter_current + BSP_TICK_MAX_CNT - *old_counter;
    *old_counter = couter_current;
    return (counter);
}

/**
 * @brief 记录归零，开始测量时间
 * @param timecnt 用来记录时间的变量
 */
void bsp_HPMesureTimeStart(bsp_timeMesure_t *timecnt)
{
    uint32_t curTick, curHPTick;
    curTick = BSP_GET_TICK();
    curHPTick = BSP_GET_HPTICK();
    timecnt->tick = BSP_TICK2HPTICK(curTick) + curHPTick;
}


/**
 * @brief 高精度测量时间
 * @param old_counter 用来记录时间的变量
 * @retval int64_t 上次使用到这次使用的时间(tick)
 */
int64_t bsp_HPMesureTime(bsp_timeMesure_t *old_counter)
{
    __IO int64_t deltaTicks, newTicks;
    __IO uint32_t curTick, curHPTick;
    curTick = BSP_GET_TICK();
    curHPTick = BSP_GET_HPTICK();
    newTicks = BSP_TICK2HPTICK(curTick) + curHPTick;
    deltaTicks = newTicks - old_counter->tick;
    old_counter->tick = newTicks;
    old_counter->deltaTick = deltaTicks;
    return deltaTicks;
}

#define buzzer_write(x) Pinx_Write(Buzzer, x);
#define buzzer_read()  Pinx_ORead(Buzzer)

uint16_t TaskBuzzerID = 0;
void TaskBuzzer(const void *arg)
{
    buzzer_write(0);
}
void Beep(uint32_t time)
{
    stimer_task_stop(TaskBuzzerID);
    stimer_task_set_interval(TaskBuzzerID, time);
    stimer_task_start(TaskBuzzerID, 1, (void*)"TaskBuzzer");
    buzzer_write(1);
}
// void board_info_output(void)
// {
// 	LOGS("JLink RTT Start\n");
// 	LOGS(LINESTR);
// 	LOG_INFO(BOARDTAG, "ProjectName :  %s\n",Board_Firmware_ProjectName);
// 	LOG_INFO(BOARDTAG, "Version     :  %s\n",Board_Firmware_Version);
// 	LOG_INFO(BOARDTAG, "Auther      :  %s\n",Board_Firmware_Auther);
// 	LOG_INFO(BOARDTAG, "SystemClock :  %d\n",Board_System_Frequnce);
// 	LOG_INFO(BOARDTAG, "Extern Flash: W25Q%d[ID:%d|FlashSize:%dKB]\n",w25qxx.BlockCount/2, w25qxx.ID, w25qxx.BlockCount * (w25qxx.BlockSize/1024));
// 	LOGS(LINESTR);
// }


/**
 * @brief 板载资源初始化
 */
void board_base_init(void)
{
    UART_BSP_Init();
    bsp_flash_config_init();
	DWT_Init();
//	HAL_ADCEx_Calibration_Start(&hadc);	
//	HAL_ADC_Start(&hadc);
    
//	HAL_TIM_Base_Start_IT(&htim17);
    HAL_TIM_Base_Start(&htim7);
    LEDx_Write(LED1, 0);
    LEDx_Write(LED2, 0);
    LEDx_Write(LED3, 0);

}

float ntc_convert_temp(uint16_t ADC_value)
{
    // 数据进入前，可先做滤波处理
    //	const float e1 = 2.718281f;
    const float Rt25 = 10000.0f; // 25摄氏度时的NTC电阻值
    const float Rs = 4700;         // 4.7K固定阻值电阻
    const float T0 = 273.15f + 25; // 转换为开尔文温度
    const float B = 3950;          // B值
    const float Ka = 273.15f;      // K值
    float Rt;                      // NTC电阻
    float temp; // 温度值
    // Rt = (3.3 - VR) * R / VR;			// 计算Rt
    Rt = Rs * ADC_value / (4095 - ADC_value);              // 计算Rt
    temp = 1.0f / (1.0f / T0 + log(Rt / Rt25) / B) - Ka; // 计算温度
    return temp;
}

/**
  * @brief  Period elapsed callback in non-blocking mode
  * @param  htim TIM handle
  * @retval None
  */
//void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
//{
////	if (htim == &htim17)
////	{
////		stimer_tick_increase();
////	}
////	if (htim == &htim6)
////	{
////		LLCC68Callback1ms();
////	}
//}

/**
  * @brief  EXTI line detection callbacks.
  * @param  GPIO_Pin: Specifies the pins connected EXTI line
  * @retval None
  */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    // UNUSED(GPIO_Pin);
    
//	if (GPIO_Pin == CMP_OVP_LV_Pin || GPIO_Pin == CMP_OVP_HV_Pin)
//	{
//		DCDC_MOS_DISABLE();
//	}
}


